// pages/myincome/hairincome/details/index.js
const app = getApp() // 引入app
import _request from '../../../../utils/request.js'
import { getTime } from '../../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    currentDate: new Date().getTime(),
    minDate: new Date(2019, 5, 1).getTime(),
    maxDate: new Date().getTime(),
    show:false,
    monthincome: 0,
    gettime: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    nowtime: '',
    page: 1,
    userObj: null,
    userlist: [],
  },

  // 关闭窗口
  onClose() {
    this.setData({
      show: false
    })
  },

  // 展示
  onShowpopup() {
    this.setData({
      show: true
    })
  },

  // 获取时间
  onInputTime(event) {
    let time = getTime(event.detail, 4)
    let gettime = getTime(event.detail, 5)
    this.setData({
      currentDate: event.detail,
      gettime: gettime,
      nowtime: time
    });
  },

  // 确定
  onType() {
    this.setData({
      show: false,
      userlist: []
    })
    this.postUserIncome(this.data.page,this.data.id, this.data.gettime, false)
  },

  
  // 获取用户收益列表
  postUserIncome(page, inviteUserId,month, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.postObj('/recomUser/recommendUserIncomeByTime', {
      userId: wx.getStorageSync('userid'),
      inviteUserId,
      roletype: "2",
      inviteUserId,
      month,
      page,
      size: 10,
    }).then((res) => {
      if (res.data.data.length > 0 && res.data.data != null) {
        let list = res.data.data.map(item => {
          item.time = getTime(item.time, 1)
          return item
        })
        this.setData({
          userlist: page > 1 ? [...this.data.userlist,...list] : list,
          monthincome: res.data.data[0].monthincome
        })
      }
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      console.log(error);
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let time = getTime(new Date().getTime(), 4)
    this.setData({
      nowtime: time
    })
    let gettime = getTime(new Date().getTime(), 5)
    this.setData({
      gettime
    })
    this.setData({
      id: options.id
    })
    // 获取美发师信息
    _request.postObj('/recomUser/recommendSingleUser', {
      userId: wx.getStorageSync('userid'),
      inviteUserId: options.id,
      roletype: "2",
    }).then((res) => {
      if (res.data.data && res.data.data != null) {
        res.data.data.inviteTime = getTime(res.data.data.inviteTime, 7)
        this.setData({
          userObj: res.data.data
        })
      }


    }).catch((error) => {
      console.log(error);
    })

    this.postUserIncome(1, options.id, gettime, false)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.postUserIncome(1, this.data.id, this.data.gettime, true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    })
    this.postUserIncome(this.data.page, this.data.id, this.data.gettime, true)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})